package com.smasher.modbus.maichong;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

import com.smasher.serialport.OnDataReceiveListener;
import com.smasher.serialport.Parity;
import com.smasher.serialport.SerialPortImpl;

import java.util.Arrays;

import android_serialport_api.Device;
import android_serialport_api.SerialPortManager;


public class SerialPortMaiChong implements SerialPortImpl {

    public static final String TAG = "maichong";

    private OnDataReceiveListener onDataReceiveListener;
    private Device device;
    private SerialPortManager manager;
    private boolean isDebug = false;

    public SerialPortMaiChong() {
        device = new Device();
    }

    /**
     * @param path     path
     * @param baud     baud
     * @param databits databits
     * @param parity   校验位, 'n': 无校验 'e': 偶校验 'o': 奇校验
     * @param stopbits stopbits
     */
    @Override
    public void init(String path, int baud, int databits, String parity, int stopbits) {
        //device.block 是否为阻塞模式打开串口节点
        device.path = path;
        device.speed = baud;
        device.dataBits = databits;
        device.stopBits = stopbits;
        if (parity.equals(Parity.NONE)) {
            device.parity = 'n';
        } else if (parity.equals(Parity.ODD)) {
            device.parity = 'o';
        } else if (parity.equals(Parity.EVEN)) {
            device.parity = 'e';
        }
    }

    @Override
    public void open() {
        Log.e(TAG, "open: ");
        manager = new SerialPortManager(device);
    }

    @Override
    public void close() {
        if (manager != null) {
            manager.closeSerialPort();
        }
    }

    @Override
    public void write(byte[] sendData) {
        if (manager != null) {
            injectLog(sendData);
            manager.sendPacket(sendData);
        }
    }

    @Override
    public void read(byte[] sendData) {
        if (manager != null) {
            injectLog(sendData);
            manager.sendPacket(sendData);
        }
    }

    private void injectLog(byte[] sendData) {
        if (isDebug) {
            if (sendData != null) {
                StringBuilder build = new StringBuilder("[");
                for (int i = 0; i < sendData.length; i++) {
                    if (i == 0) {
                        build.append(sendData[i]);
                    } else {
                        build.append(",").append(sendData[i]);
                    }
                }
                build.append("]");
                Log.d(TAG, build.toString());
            }
        }
    }

    @Override
    public void addDataReceiveListener(OnDataReceiveListener listener) {
        onDataReceiveListener = listener;
        if (manager != null) {
            manager.setOnDataReceiveListener((bytes, i) -> {
                Log.d(TAG, "bytes: " + Arrays.toString(bytes));
                if (onDataReceiveListener != null) {
                    onDataReceiveListener.onDataReceive(bytes, i);
                }
            });
        }
    }

    @Override
    public String[] getAllDevicesPath(Context context) {
        Resources resources = context.getResources();
        return resources.getStringArray(R.array.maichong_port);
    }

    @Override
    public void setDebug(boolean b) {
        isDebug = b;
    }

}
